﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Security;
using Core.Library;
using DataObject;
using DataObject.Members;
using DataObject.Photo;
using DataObject.QA;
using DataObject.Ref;
using DataObject.Vote;

namespace Event.Handlers
{
    /// <summary>
    /// Summary description for EventHandler
    /// </summary>
    public class EventHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string command = "";
            if (context.Request.RequestType == "POST")
            {
                if (context.Request["command"] != null)
                {
                    command = context.Request["command"];
                }
                else
                {
                    return;
                }
            }
            else
            {
                if (context.Request.QueryString["command"] != null)
                {
                    command = context.Request.QueryString["command"];
                }
                else
                {
                    return;
                }
            }

            switch (command.ToLower())
            {
                case "getphotoalbum":
                    GetPhotoAlbum(context);
                    break;
                    break;
                case "boxnhung":
                    BoxNhung(context);
                    break;
            }
        }

        private void BoxNhung(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            var callback = !string.IsNullOrEmpty(context.Request.Params["callback"])
                ? context.Request.Params["callback"].Trim()
                : string.Empty;
                string strHtml = MembersCache.Member_HighlightHomePage_BoxNhung(100, RefCache.Ref_GetCurrentRound(), 150, 150);
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                strHtml = serializer.Serialize(strHtml);
                strHtml = string.Format("{0}({1})", callback, strHtml);
                context.Response.Write(strHtml);
        }

        public void GetPhotoAlbum(HttpContext context)
        {
            if (context.Request[Utils.PageIndex] != null && context.Request[Utils.Round] != null && context.Request[Utils.PageSize] != null)
            {
                int pageIndex = ConvertUtility.ConvertTo<int>(context.Request[Utils.PageIndex]);
                int pageSize = ConvertUtility.ConvertTo<int>(context.Request[Utils.PageSize]);
                int round = ConvertUtility.ConvertTo<int>(context.Request[Utils.Round]);

                DataTable dt = PhotoCache.Photos_ListAllPhotoPaging(round, pageIndex, pageSize);

                if (dt != null && dt.Rows.Count > 0)
                {
                    string strJsonReturn = DataTableToJSON_Customize(dt);
                    context.Response.ContentType = "application/x-javascript";
                    context.Response.Write(strJsonReturn);
                }
            }
        }

        public static string DataTableToJSON_Customize(DataTable table)
        {
            List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();

            foreach (DataRow row in table.Rows)
            {
                Dictionary<string, object> dict = new Dictionary<string, object>();
                int id = ConvertUtility.ConvertTo<int>(row[Constants.Table_Members.Member_ID]);
                var round = ConvertUtility.ConvertTo<int>(row[Constants.Table_Members.Member_InRound]);
                foreach (DataColumn col in table.Columns)
                {
                    if (col.ColumnName == Constants.Table_Members.Vote)
                        dict[col.ColumnName] = VoteCache.Members_GetVoteByRound(id, round);
                    else dict[col.ColumnName] = row[col];
                }
                list.Add(dict);
            }
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            return serializer.Serialize(list);
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}